<script setup lang="ts">
import { getWXUserListAPI } from '@/api/account'
import type { userItem } from '@/types/member'
import { ref } from 'vue'
import { busListStore } from '@/stores'
import { useMemberStore } from '@/stores'
import { delBusinessPeopleAPI, getAppUserListByBusIdAPI, postBusinessPeopleAPI } from '@/api/business'
import { onLoad } from '@dcloudio/uni-app'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

const busStore = busListStore()
const busItem = busStore.busList[0]
const userMember = useMemberStore().profile
// tabs 数据
const orderTabs = ref([
  { orderState: 0, title: '绑码人员' },
  { orderState: 1, title: '兑奖人员' },
])

// 获取页面参数
const query = defineProps<{
  type: string
}>()

// 高亮下标
const activeIndex = ref(orderTabs.value.findIndex((v) => v.orderState === Number(query.type)))

// 取消搜索
const cancel = () => {
  peopleList.value = []
}
// 搜索列表显示隐藏
const searchActive = ref(false)

// 搜索
const searchValue = ref('')
const peopleList = ref<userItem[]>([])
const search = async () => {
  const res = await getWXUserListAPI(searchValue.value)
  if (res.data.records.length > 0) {
    peopleList.value = res.data.records
    searchActive.value = true
  } else {
    uni.showToast({
      title: '没有搜索到结果，请重新尝试',
      icon: 'none',
    })
  }
}
// 清楚搜索文字
const clear = () => {
  searchValue.value = ''
  peopleList.value = []
}

// 添加绑码人员
const addPeisong = async (userId: string) => {
  let addPerson = {
    userId,
    roleCode: 'delivery_user',
    busId: busStore.busList[0].id,
  }
  await postBusinessPeopleAPI(addPerson)

  uni.showToast({
    title: '添加绑码人员成功',
    icon: 'success',
  })
  getAppUserListByBusId()
}

// 获取绑码人员
const userPeisong = ref<userItem[]>([])
const getAppUserListByBusId = async () => {
  const res = await getAppUserListByBusIdAPI('delivery_user', busStore.busList[0].id)
  userPeisong.value = res.data
}

// 获取绑码人员
const userBangma = ref<userItem[]>([])
const getAppUserBMListByBusId = async () => {
  const res = await getAppUserListByBusIdAPI('cash_user', busStore.busList[0].id)
  userBangma.value = res.data
}

// 添加绑码人员
const addBangma = async (userId: string) => {
  let addPerson = {
    userId,
    roleCode: 'cash_user',
    busId: busStore.busList[0].id,
  }
  await postBusinessPeopleAPI(addPerson)
  uni.showToast({
    title: '添加绑码人员成功',
    icon: 'success',
  })
  getAppUserBMListByBusId()
}
// 删除绑码人员
const delPeoplePeisong = async (userId:string) => {
  let addPerson = {
    userId,
    roleCode: 'delivery_user',
    busId: busStore.busList[0].id,
  }
  await delBusinessPeopleAPI(addPerson)
  uni.showToast({
    title: '删除绑码人员成功',
    icon: 'success',
  })
  getAppUserListByBusId()
}
// 删除绑码人员
const delPeopleBangma = async (userId: string) => {
  let addPerson = {
    userId,
    roleCode: 'cash_user',
    busId: busStore.busList[0].id,
  }
  await delBusinessPeopleAPI(addPerson)
  uni.showToast({
    title: '删除绑码人员成功',
    icon: 'success',
  })
  getAppUserBMListByBusId()
}

onLoad(() =>{
  getAppUserListByBusId()
  getAppUserBMListByBusId()
})
</script>

<template>
  <view class="viewport">
    <!-- tabs -->
    <view class="tabs">
      <text
        class="item"
        v-for="(item, index) in orderTabs"
        :key="item.title"
        @tap="activeIndex = index"
      >
        {{ item.title }}
      </text>
      <!-- 游标 -->
      <view class="cursor" :style="{ left: activeIndex * 50 + 10 + '%' }"></view>
    </view>
    <!-- 搜索得到的人员列表 -->
    <view class="search_list">
      <!-- 搜索框 -->
      <uni-search-bar
        @confirm="search"
        :focus="true"
        v-model="searchValue"
        @cancel="cancel"
        @clear="clear"
      >
      </uni-search-bar>
      <view v-if="peopleList.length>0">
        <view class="sear_item" v-for="item in peopleList" :key="item.userId">
          <view class="left">
            <view class="name">{{ item.username }}</view>
            <view class="phone">{{ item.phone }}</view>
          </view>
          <view class="btn_group">
            <button
              size="mini"
              style="color: #fff; background-color: #18bc37"
              @tap="addPeisong(item.userId!)"
            >
              <uni-icons type="plus" size="14" color="#fff"></uni-icons> 绑码员
            </button>
            <button
              size="mini"
              style="color: #fff; background-color: #2979ff"
              @tap="addBangma(item.userId!)"
            >
              <uni-icons type="plus" size="14" color="#fff"></uni-icons> 兑奖员
            </button>
          </view>
        </view>
      </view>
    </view>
    <!-- 滑动容器 -->
    <swiper class="swiper" :current="activeIndex" @change="activeIndex = $event.detail.current">
      <!-- 滑动项 -->
      <swiper-item>
        <!-- 列表 -->
        <scroll-view scroll-y class="orders">
          <view class="list">
            <view class="title">绑码人员列表</view>
            <view class="people_item">
              <view class="name">{{ busItem.contacts }}</view>
              <view class="phone">{{ busItem.contactsPhone }}</view>
              <button size="mini" type="default" disabled>默认</button>
            </view>
            <view class="people_item" v-for="item in userPeisong" :key="item.userId">
              <view class="name">{{ item.username }}</view>
              <view class="phone">{{ item.phone }}</view>
              <button size="mini" type="warn" @tap="delPeoplePeisong(item.userId!)">
                <uni-icons type="close" size="14" color="#fff"></uni-icons> 删除
              </button>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
      <!-- 滑动项 -->
      <swiper-item>
        <!-- 列表 -->
        <scroll-view scroll-y class="orders">
          <view class="list">
            <view class="title">兑奖人员列表</view>
            <view class="people_item" v-for="item in userBangma" :key="item.userId">
              <view class="name">{{ item.username }}</view>
              <view class="phone">{{ item.phone }}</view>
              <button size="mini" type="warn" @tap="delPeopleBangma(item.userId!)">
                <uni-icons type="close" size="14" color="#fff"></uni-icons> 删除
              </button>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
    </swiper>
  </view>
</template>
<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}
.search_list {
  padding: 15px;
  .sear_item {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid #eee;
    .left {
      flex: 1;
      font-size: 26rpx;
      line-height: 40rpx;
      display: flex;
      align-items: center;
      .name {
        width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: inherit;
      }
      .phone {
        width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: inherit;
      }
    }
    .btn_group {
      button {
        margin-left: 5px;
        padding: 0 5px;
        font-size: 24rpx;
      }
    }
  }
}
.people_item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 15px;
  border-bottom: 1px solid #eee;
  .name, .phone {
    font-size: 28rpx;
    width: 90px;
  }
  .weixin {
    font-size: 28rpx;
    width: 120px;
    text-align: right;
  }
  button {
    margin: 0;
    width: 60px;
    padding-left: 0;
    padding-right: 0;
  }
}
.list .title {
  display: flex;
  justify-content: space-between;
  padding-right: 5px;
  align-items: center;
  padding-left: 15px;
  height: 40px;
  background-color: #27ba9b;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
}

// tabs
.tabs {
  display: flex;
  justify-content: space-around;
  line-height: 60rpx;
  margin: 0 10rpx;
  background-color: #fff;
  box-shadow: 0 4rpx 6rpx rgba(240, 240, 240, 0.6);
  position: relative;
  z-index: 9;

  .item {
    flex: 1;
    text-align: center;
    padding: 20rpx;
    font-size: 28rpx;
    color: #262626;
  }

  .cursor {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 33.33%;
    height: 6rpx;
    padding: 0 50rpx;
    background-color: #27ba9b;
    /* 过渡效果 */
    transition: all 0.4s;
  }
}

// swiper
.swiper {
  background-color: #f7f7f8;
}
</style>
